package com.towker.frame.controller.sys;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("/image")
@SessionAttributes("currUser")
public class UploadController
{
	@RequestMapping(value = "/upload2")
	public String upload2(MultipartHttpServletRequest request, @RequestParam("text")
	String text // 页面上的控件值
	) throws Exception
	{
		System.out.println("::::");
		String str = UploadController.class.getResource("/").getPath();
		System.out.println(str);
		String path = UploadController.class.getResource("/").getPath().split("WEB-INF")[0] + "upload/images/"; // 获取站点下的绝对磁盘路径
		System.out.println("[ text ] : " + text); // 打印 页面上的控件值
		String txt = request.getParameter("text");
		System.out.println(txt);
		MultiValueMap<String, MultipartFile> map = request.getMultiFileMap();
		@SuppressWarnings("unused")
		Set<String> set = map.keySet();
		List<MultipartFile> files = request.getFiles("file");
		for (int i = 0; i < files.size(); i++)
		{
			if (!files.get(i).isEmpty())
			{
				System.out.println(files.get(i).getOriginalFilename());
				byte[] bytes = files.get(i).getBytes();
				FileOutputStream fos = new FileOutputStream(path + files.get(i).getOriginalFilename()); // 写入文件
				fos.write(bytes);
				fos.close();
			}
			System.out.println("Ok");
		}
		return "redirect:/view/jsp/product/uploadForm.jsp";
	}
}
